import java.io.*;
import java.util.*;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class TequilaSunlightControllerApp extends JApplet
{
    private static TequilaSunlightController 
        tsc;    
    private JButton 
        b1 = new JButton("StartBlink"), 
        b2 = new JButton("StopBlink"),
        b3 = new JButton("StartSequence"),
        b4 = new JButton("On"),
        b5 = new JButton("Off");        
    private DefaultComboBoxModel 
        lampComboModel,
        sequenceComboModel,
        blinkComboModel;
	private JComboBox 
        lampCombo,
        sequenceCombo,
        blinkCombo;

    class ButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            String name = ((JButton) e.getSource()).getText();
            int lamp = Integer.parseInt( ( String ) lampCombo.getSelectedItem() );
            int sequence = Integer.parseInt( ( String ) sequenceCombo.getSelectedItem() );
            int blink_period = Integer.parseInt( ( String ) blinkCombo.getSelectedItem() );
            
            try
            {            
                if( name.equals( "StartBlink" ) )
                {
                    tsc.startBlink( lamp, blink_period / 100 );
                }
                else if( name.equals( "StopBlink" ) )
                {
                    tsc.stopBlink( lamp );
                }
                else if( name.equals( "StartSequence" ) )
                {
                    tsc.startSequence( lamp, sequence );
                }  
                else if( name.equals( "On" ) )
                {
                    tsc.setLamp( lamp, true );
                }
                else if( name.equals( "Off" ) )
                {
                    tsc.setLamp( lamp, false );
                }                  
            }
            catch( Exception ex )
            {
                System.out.println( "Exception : " + ex );
            } 
            
        }
    }

    private ButtonListener buttonListener = new ButtonListener();

    public void init() 
    {
        b1.addActionListener( buttonListener );
        b2.addActionListener( buttonListener );
        b3.addActionListener( buttonListener );
        b4.addActionListener( buttonListener );
        b5.addActionListener( buttonListener );
        
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());

        lampComboModel = new DefaultComboBoxModel();
		lampComboModel.addElement("0");
		lampComboModel.addElement("1");
		lampComboModel.addElement("2");
		lampCombo = new JComboBox( lampComboModel );
        
        sequenceComboModel = new DefaultComboBoxModel();
		sequenceComboModel.addElement("1");
		sequenceComboModel.addElement("2");
        sequenceComboModel.addElement("3");
        sequenceComboModel.addElement("4");
        sequenceComboModel.addElement("5");
        sequenceComboModel.addElement("6");
        sequenceComboModel.addElement("7");
        sequenceComboModel.addElement("8");
        sequenceComboModel.addElement("9");
        sequenceComboModel.addElement("10");
 		sequenceComboModel.addElement("11");
		sequenceComboModel.addElement("12");
        sequenceComboModel.addElement("13");
        sequenceComboModel.addElement("14");
        sequenceComboModel.addElement("15");
        sequenceComboModel.addElement("16");
        sequenceComboModel.addElement("17");
        sequenceComboModel.addElement("18");
        sequenceComboModel.addElement("19");       
        sequenceCombo = new JComboBox( sequenceComboModel );
        
        blinkComboModel = new DefaultComboBoxModel();
		blinkComboModel.addElement("100");
		blinkComboModel.addElement("200");
        blinkComboModel.addElement("300");
        blinkComboModel.addElement("400");
        blinkComboModel.addElement("500");
        blinkComboModel.addElement("600");
        blinkComboModel.addElement("700");
        blinkComboModel.addElement("800");
        blinkComboModel.addElement("900");
		blinkComboModel.addElement("1000");
		blinkComboModel.addElement("1100");
        blinkComboModel.addElement("1200");
        blinkComboModel.addElement("1300");
        blinkComboModel.addElement("1400");
        blinkComboModel.addElement("1500");
        blinkComboModel.addElement("1600");
        blinkComboModel.addElement("1700");
        blinkComboModel.addElement("1800");
        blinkComboModel.addElement("1900"); 
        blinkComboModel.addElement("2000"); 
        blinkCombo = new JComboBox( blinkComboModel );        
        
        cp.add( new JLabel("Lamp:") );
        cp.add(lampCombo);
        cp.add( new JLabel("Sequence:") );
        cp.add(sequenceCombo); 
        cp.add( new JLabel("Blink period:") );
        cp.add(blinkCombo);        
        cp.add(b1);
        cp.add(b2);
        cp.add(b3);
        cp.add(b4);
        cp.add(b5);      
    }
   
    
    
    public static void run(JApplet applet, int width, int height) {
        JFrame frame = new JFrame( "TequilaSunlightController" );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
    
    public static void main ( String[] args ) 	
    {
        System.out.println ("TequilaSunlightController badly coded in Java");        
        
        try
        {        
            tsc = new TequilaSunlightController();
            boolean portOk = false;
            
            while( !portOk )
            {
                tsc.initialize();
                portOk = tsc.selectPort( "COM9" );
            }
            
            System.out.println ("TequilaSunlightController Ok!");
            tsc.ping();
            run( new TequilaSunlightControllerApp(), 210, 185 );
            
            /*
            tsc.startBlink( 0, 5 );
            tsc.startBlink( 1, 5 );
            tsc.startBlink( 2, 5 );
            tsc.sleep( 5000 );
            tsc.stopBlink( 0 );
            tsc.stopBlink( 1 );
            tsc.stopBlink( 2 );          
            tsc.startSequence( 2, 8 );
            */            
        }
        catch( Exception e )
        {
            System.out.println( "Exception : " + e );
        }      

    }    
}

